import unittest
from api.login_api import LoginApi
from common.db_util import DBUtil
from parameterized import parameterized
from config import BASE_DIR
from common.read_json_data import build_data
from api.e001 import EmpApi
from common.verify_result import common_assert

class TestEmp(unittest.TestCase):

    au_headers = None
    mobile = '11122226666'

    @classmethod
    def setUpClass(cls) -> None:
        print('类前置')
        body={"mobile":"13800000002","password":"123456"}
        login_obj = LoginApi()
        TestEmp.au_headers = login_obj.get_au_headers(body)
        print(f'获取的请求头为{TestEmp.au_headers}')

        sql = f"delete from bs_user where mobile='{TestEmp.mobile}'"
        n = DBUtil.iud_data(sql)
        print(n)
    @classmethod
    def tearDownClass(cls) -> None:
        print('类后置')
        sql = f"delete from bs_user where mobile='{TestEmp.mobile}'"
        n = DBUtil.iud_data(sql)
        print(n)

    @parameterized.expand(build_data(BASE_DIR + '/data/emp.json'))
    def test_emp(self,desc,add_body,modify_body,expect_result):
        print(desc)
        add_body['mobile']= TestEmp.mobile
        print(f'增加员工请求体为{add_body}')
        print(f'修改员工请求体为{modify_body}')
        print(f'预期结果为{expect_result}')
        emp_obj = EmpApi()
        resp = emp_obj.add_emp(add_body,TestEmp.au_headers)
        json_data = resp.json()
        print(f'添加员工结果为{json_data}')
        common_assert(self,expect_result,resp)
        emp_id = json_data.get('data').get('id')
        print(f'员工id为{emp_id}')

        resp = emp_obj.query_emp(emp_id, TestEmp.au_headers)
        json_data = resp.json()
        print(f'查询员工结果为{json_data}')
        common_assert(self, expect_result, resp)

        resp = emp_obj.modify_emp(add_body, emp_id,TestEmp.au_headers )
        json_data = resp.json()
        print(f'修改员工结果为{json_data}')
        common_assert(self, expect_result, resp)

        resp = emp_obj.delete_emp(emp_id, TestEmp.au_headers)
        json_data = resp.json()
        print(f'删除员工结果为{json_data}')
        common_assert(self, expect_result, resp)